package manager.webservice {
	import com.debug.TDebug;
	import com.windows.AlertWindow;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.System;
	
	import manager.xml.XMLConfig;

	/**
	 * WebServices的方法及参数
	 * 方法名称
	 * 参数名称(有顺序)
	 * 回调的涵数
	 * @author pethan
	 */
	public class FunctionData extends EventDispatcher {
		public var functionName:String;
		public var arugments:Vector.<Object> = new Vector.<Object>();
		public var iwebService:IWebServices;
		private var callBack:Function;
		
		private var request:URLRequest;      /*Request请求*/
		private var webLoader:URLLoader;     /*urlLoader*/
		private var variables:URLVariables;  /*UrlVariables*/

		public function FunctionData(){
		}

		/**
		 * 进行调用webservices
		 * @param _functionName 方法名称(webserivces)
		 * @param _argu 参数(数组)
		 * @param iService 调用这个的对象
		 * @param webUrl web地址,如果为空,就调用默认的,这样就可以支持同时多个地方进行调用webservices
		 * @param callBack 回调涵数
		 */
		public function setData(_methodName:String, _args:Vector.<Object>,
								iService:IWebServices, webURL:String = "", callBack:Function = null):void {
			System.useCodePage = false; //由于FusionChart会引起乱码
			
			this.functionName = _methodName;
			this.arugments = _args;     //参数值数组
			iwebService = iService;
			this.callBack = callBack;
			var url:String = "";
			
			for (var i:int = 0; i < this.arugments.length; i++){
				if (i == 0){
					url += this.arugments[i].name + "=" + this.arugments[i].data;
				} else {
					url += "&" + this.arugments[i].name + "=" + this.arugments[i].data;
				}
			}
			if (webURL == ""){
				webURL = XMLConfig.getInstance().wsdl;
			}
			request = new URLRequest(webURL + "/" + this.functionName);
			request.method = URLRequestMethod.POST;
			webLoader = new URLLoader();
			webLoader.addEventListener(Event.COMPLETE, xmlLoaded);
			webLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			webLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);

			if(url.length!=0){
				variables = new URLVariables(url);
				request.data = variables;
			}
			
			TDebug.trace('Funciton:' + _methodName + "webUrl:" + webURL + "/" + this.functionName + "?" + url);
			webLoader.load(request);
		}

		private function onError(event:IOErrorEvent):void {
			trace("IO错误代码:10001");
		}

		private function onSecurityError(event:SecurityErrorEvent):void {
			trace("沙箱错误代码:10002");
		}

		private function xmlLoaded(event:Event):void {
			try {
			   //var xmldata:XML=new XML(decodeURI(event.target.data));
				var xmldata:XML = new XML(event.target.data);
			} catch (e:Error){
				//TDebug.trace('错误xml数据' + event.target.data);
				trace("错误代码:10003");
			}
			this.dispatchEvent(new ServicesEvent(xmldata, functionName));
			
			if (callBack != null){
				callBack.call(null, xmldata); //传递给回调函数的参数
			} else {
				iwebService.updateData(xmldata);
			}
		}

		/**
		 * 销毁data里面的相关对象
		 */
		public function dispose():void {
			this.callBack = null;
			if (this.arugments.length > 0){
				this.arugments.splice(0, this.arugments.length);
			}
			this.arugments = null;
			webLoader.removeEventListener(Event.COMPLETE, xmlLoaded);
			webLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			webLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			webLoader = null;
			this.request = null;
			this.variables = null;
		}
	}
}